﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace SZSLSystem.DAL
{
	/// <summary>
	///数据访问类 Project (create by HC 2013/1/14 22:28:41)
	/// </summary>
	public class Project
	{
		public Project(){}
		public bool Exists(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(OID) from Project");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.Exists(strSql.ToString(), parameters);
		}

		public int Delete(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from Project");
			strSql.Append(" where OID=@OID  ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public int Add(SZSLSystem.Model.Project model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into Project( ");
			strSql.Append("[OID],[ProjectName],[ProjectAddress],[BuildCompany],[DesignCompany],[ConstructionCompany],[MarketingTel],[MarketingCompany],[QualitativeInspectCompany],[PropertyManagement])");
			strSql.Append("values (@OID,@ProjectName,@ProjectAddress,@BuildCompany,@DesignCompany,@ConstructionCompany,@MarketingTel,@MarketingCompany,@QualitativeInspectCompany,@PropertyManagement)");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@ProjectName",SqlDbType.VarChar,500),
					new SqlParameter ("@ProjectAddress",SqlDbType.VarChar,500),
					new SqlParameter ("@BuildCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@DesignCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@ConstructionCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@MarketingTel",SqlDbType.VarChar,50),
					new SqlParameter ("@MarketingCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@QualitativeInspectCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@PropertyManagement",SqlDbType.VarChar,500)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.ProjectName;
			parameters[2].Value = model.ProjectAddress;
			parameters[3].Value = model.BuildCompany;
			parameters[4].Value = model.DesignCompany;
			parameters[5].Value = model.ConstructionCompany;
			parameters[6].Value = model.MarketingTel;
			parameters[7].Value = model.MarketingCompany;
			parameters[8].Value = model.QualitativeInspectCompany;
			parameters[9].Value = model.PropertyManagement;
			object obj = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		public  SZSLSystem.Model.Project GetModel(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select top 1 [OID],[ProjectName],[ProjectAddress],[BuildCompany],[DesignCompany],[ConstructionCompany],[MarketingTel],[MarketingCompany],[QualitativeInspectCompany],[PropertyManagement] from Project");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			SZSLSystem.Model.Project model=new SZSLSystem.Model.Project();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataRow row = ds.Tables[0].Rows[0];
						model.OID=row["OID"].ToString();
						model.ProjectName=row["ProjectName"].ToString();
						model.ProjectAddress=row["ProjectAddress"].ToString();
						model.BuildCompany=row["BuildCompany"].ToString();
						model.DesignCompany=row["DesignCompany"].ToString();
						model.ConstructionCompany=row["ConstructionCompany"].ToString();
						model.MarketingTel=row["MarketingTel"].ToString();
						model.MarketingCompany=row["MarketingCompany"].ToString();
						model.QualitativeInspectCompany=row["QualitativeInspectCompany"].ToString();
						model.PropertyManagement=row["PropertyManagement"].ToString();

				return model;
			}
			else
			{
				return null;
			}
		}

		public int Update(SZSLSystem.Model.Project model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update Project set ");
			strSql.Append("[OID]=@OID,");
			strSql.Append("[ProjectName]=@ProjectName,");
			strSql.Append("[ProjectAddress]=@ProjectAddress,");
			strSql.Append("[BuildCompany]=@BuildCompany,");
			strSql.Append("[DesignCompany]=@DesignCompany,");
			strSql.Append("[ConstructionCompany]=@ConstructionCompany,");
			strSql.Append("[MarketingTel]=@MarketingTel,");
			strSql.Append("[MarketingCompany]=@MarketingCompany,");
			strSql.Append("[QualitativeInspectCompany]=@QualitativeInspectCompany,");
			strSql.Append("[PropertyManagement]=@PropertyManagement");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@ProjectName",SqlDbType.VarChar,500),
					new SqlParameter ("@ProjectAddress",SqlDbType.VarChar,500),
					new SqlParameter ("@BuildCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@DesignCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@ConstructionCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@MarketingTel",SqlDbType.VarChar,50),
					new SqlParameter ("@MarketingCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@QualitativeInspectCompany",SqlDbType.VarChar,500),
					new SqlParameter ("@PropertyManagement",SqlDbType.VarChar,500)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.ProjectName;
			parameters[2].Value = model.ProjectAddress;
			parameters[3].Value = model.BuildCompany;
			parameters[4].Value = model.DesignCompany;
			parameters[5].Value = model.ConstructionCompany;
			parameters[6].Value = model.MarketingTel;
			parameters[7].Value = model.MarketingCompany;
			parameters[8].Value = model.QualitativeInspectCompany;
			parameters[9].Value = model.PropertyManagement;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select [OID],[ProjectName],[ProjectAddress],[BuildCompany],[DesignCompany],[ConstructionCompany],[MarketingTel],[MarketingCompany],[QualitativeInspectCompany],[PropertyManagement]");
			strSql.Append(" FROM Project ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(int top,string strWhere,string fieldOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select");
			if(top>0)
			{
				strSql.Append(" top "+top.ToString());
			}
			strSql.Append(" [OID],[ProjectName],[ProjectAddress],[BuildCompany],[DesignCompany],[ConstructionCompany],[MarketingTel],[MarketingCompany],[QualitativeInspectCompany],[PropertyManagement]");
			strSql.Append(" FROM Project ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
				strSql.Append(" order by "+fieldOrder);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(string showFields,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+showFields);
			strSql.Append(" FROM Project ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public object GetSingle(string fieldName,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+fieldName);
			strSql.Append(" FROM Project ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.GetSingle(strSql.ToString());
		}

	}
}